import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import '../../models/user_storm_info.model.dart';
import '../../store/user.dart';
import '../index.dart';
Future<void> login(context, String phone, {Response<dynamic>? captchaInfo}) async {
  SmartDialog.showLoading(msg: '登录中...');
  ApiService apiService = ApiService();
  try {
    if (captchaInfo == null || captchaInfo.data == null) captchaInfo = await apiService.get('/frontCaptcha');
    print(captchaInfo);
    print('aaa');
    final query = {
      'captcha': captchaInfo.data['captcha'],
      'uuid': captchaInfo.data['uuid'],
      'phone': phone,
    };
    final response = await apiService.post('/system/info/login', data: query);
    print(response);
    // final userInfo = {
    //   'id': response.data['id'],
    //   'phone': phone,
    //   'token': response.data['token']
    // };

    UserInfoModel userInfo = UserInfoModel(token: response.data['token'], phone: phone);
    await UserStore.saveUserInfo(userInfo);
    await Navigator.pushNamedAndRemoveUntil(
      context,
      '/',
          (route) => false, // 清除所有路由历史
    );
  } catch (e) {
    print("登录失败：$e");
  } finally {
    // 确保加载框一定会被关闭
    SmartDialog.dismiss();
  }
}
